Thread: operator[] difference of const and non-const

  1. #16
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    And a function that returns an iterator creates a temporary.
    I'm fairly certain that you misunderstood what laserlight said.

    The standard containers return different types (conceptually) of iterators by value depending on whether or not the container is constant in the given situation.

    That the block content are fundamentally different, not just superficial differences?
    O_o

    Being "const correct" isn't a superficial difference.

    Soma

  2. #17
    Registered User DynV's Avatar
    Join Date
    Jul 2012
    Location
    Montreal, Canada
    Posts
    20
    Quote Originally Posted by phantomotap View Post
    Quote Originally Posted by DynV View Post
    That the block content are fundamentally different, not just superficial differences?
    O_o

    Being "const correct" isn't a superficial difference.
    This was the important part
    block content
    perhaps the compiled versions are (much) different but certainly not the blocks as far as what I mentioned in #12.

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The const version of the operator is read only. It's a semantic difference. The const operator will be used unless it specifically cannot be used -- like when the object is an lvalue -- and your constant objects will still be able to use the operator without violating their constness. It should be pretty easy, if you know what the subscript operator does, to imagine a bad line of code that changes an object that isn't supposed to be changed. Writing two operators varying in constness will prevent such code from being compiled.

  4. #19
    Registered User DynV's Avatar
    Join Date
    Jul 2012
    Location
    Montreal, Canada
    Posts
    20
    Quote Originally Posted by whiteflags View Post
    It should be pretty easy, if you know what the subscript operator does, to imagine a bad line of code that changes an object that isn't supposed to be changed. Writing two operators varying in constness will prevent such code from being compiled.
    Sorry but I don't get this. :|

    Would someone please just make me an example (or give URLs which someone with no prior knowledge of const & non-const difference would understand), or a few if there's different types, where const & non-const version of operator[] would have different block content. It would be most helpful.

  5. #20
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Dude, just stop trying to fight it!

    The code between the braces is irrelevant; the issue of having both overloads is about "const correctness".

    The code may or may not be different depending on how the facility is intended to behave, but that's a meaningless focus of your confusion because these differences would still be about making the code "const correct".

    Consider the often implementation of the standard containers (as I've already referenced); the two overloads return either completely different or semantically (conceptually) different types. The code in the blocks may still look identical but because the functions are returning different types they behave differently. That is all that matters. The one overload returns an iterator which is itself mutable and which can be used to mutate the contents of the container. The other overload (the `const' one) returns an iterator which is itself mutable but which can not be used to mutate the contents of the container.

    You are focusing on a non-issue; having both overloads is about being "const correct". If the code needs to be different to make that possible it certainly can be different. If the code doesn't need to be different to make that possible it certainly does not have to be different.

    Soma

  6. #21
    Registered User DynV's Avatar
    Join Date
    Jul 2012
    Location
    Montreal, Canada
    Posts
    20
    Quote Originally Posted by phantomotap View Post
    You are focusing on a non-issue
    Yeah I'm just like that and I'm still focused on it, just like my OP mentioned:
    Quote Originally Posted by DynV View Post
    my question will remain the same, is there a difference in the code between the 2 beside something no-so-important like one display an error [...] and the other make a log
    I know what you're saying about const correctness, that the const version should be the most important version and only to add the non-const version if needed but I'm still wondering what kind of code could be different ; not the signature part, the block part. Because for now having 2 versions of the same block, in my disarranged mind, only has use if 1 of the 2 should be forbidden, ie:
    Code:
    /* ..*/ operator[](/* ..*/) {
        assert(false);
    }
    Last edited by DynV; 07-10-2012 at 08:49 PM. Reason: about const correctness

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I am of the opinion that you should implement both versions, because you need them.

    If you didn't have both versions, how wrong would this program be?
    Code:
    #include <vector>
    #include <iostream>
    
    int main()
    {
        const std::vector<int> foo(10, 42); // create ten 42s
        foo[1] = 40;                        // can't change them!
    
        std::vector<int> bar(10, 42);
        bar[1] = 40;                   // except when they're not const
    }
    It is extremely likely that both versions of the subscript operator are apparently the same. This is because the subscript operator has a simple meaning.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by King Mir
    A temporary binds to a const reference, never a non-const reference. And a function that returns an iterator creates a temporary.
    And the parameter of both overloads of operator[] is of const reference type, hence what you are talking about does not matter.

    Quote Originally Posted by King Mir
    Member function lookup is a little different; you can call a non const method on a temporary.
    Exactly.

    EDIT:
    Quote Originally Posted by whiteflags
    If you didn't have both versions, how wrong would this program be?
    Also, if your container is designed to be immutable, then you might provide only the const overload. As you should have seen earlier in this thread, it would still work when called with a non-const container.
    Last edited by laserlight; 07-10-2012 at 09:06 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User DynV's Avatar
    Join Date
    Jul 2012
    Location
    Montreal, Canada
    Posts
    20
    Quote Originally Posted by whiteflags View Post
    t is extremely likely that both versions of the subscript operator are apparently the same. This is because the subscript operator has a simple meaning.
    I will assume this is what I was looking for ; that it's extremely unlikely that what my OP suggested (block of const & non-const operator[] be different) happen. So I might was well put that in my mind for when I get very advanced in C++.

    Thanks again

    Although I'm not so happy about you because after watching your signature earlier, I spent 30 minutes listening to chiptune.
    Last edited by DynV; 07-10-2012 at 10:04 PM.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Just remember though, that some classes will intentionally only have say the const version.
    e.g. if you had a class that wrapped a read-only connection to a database.
    So it's not about whether they have the same behaviour or not, it's about saying which of the two pieces of functionality is your class meant to provide. Unfortunately for those classes that intend to expose both, there can be some code duplication. It's usually small though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #26
    Registered User DynV's Avatar
    Join Date
    Jul 2012
    Location
    Montreal, Canada
    Posts
    20
    Quote Originally Posted by iMalc View Post
    Just remember though, that some classes will intentionally only have say the const version.
    e.g. if you had a class that wrapped a read-only connection to a database.
    That's what I understood from the concept right before creating this thread but the more examples, the merrier.

    I was about to forget: I did search on the topic before creating a new thread but the search algorithm isn't what I'm used to ; there was ~1 related topic per result page, as seen in the attached image.
    Attached Images Attached Images operator[] difference of const and non-const-cprogramming-search-cplusplus-operator[]-jpg 
    Last edited by DynV; 07-11-2012 at 08:53 AM.

  12. #27
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm not really sure how you searched. The search algorithm is actually pretty good. But you have to use advance search if you want to control where you search. In my experience, search by default looks for the keywords in the whole post, in all of the open forums. If you want to search by title in the C++ Programming forum or other things like that, go to advanced.

  13. #28
    Registered User DynV's Avatar
    Join Date
    Jul 2012
    Location
    Montreal, Canada
    Posts
    20
    I used advanced, as the search criteria show on top of the image. I also use Search Titles Only, either one of us had luck going for or against, I'm missing something unusual (I'm not a stranger to such search) or the algorithm changed.

  14. #29
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The forum software does change rather constantly. The search method that works best for me is in advanced like you've been using. I like to search single content type, too, because that makes sense to me. I haven't really figured out what multiple content type searches are supposed to do, unless you want to search in, like, visitor messages. And who cares about that. Anyway, the default is for multiple content type searches so you will either need to figure out the form on that page, or switch to single content type. From there you only need to figure out the form to search successfully. Maybe you've already done this

    If I repeat your search on the single content type tab, I get this: http://cboard.cprogramming.com/searc...earchid=499297 -- four pages of operator overloading threads. If you continue to have significant trouble, you could always pm a moderator.
    Last edited by whiteflags; 07-11-2012 at 11:58 AM.

  15. #30
    Registered User DynV's Avatar
    Join Date
    Jul 2012
    Location
    Montreal, Canada
    Posts
    20
    Quote Originally Posted by whiteflags View Post
    nice link
    vBulletin Message

    Sorry - no matches. Please try some different terms.



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is the difference between #define n 10 and const int n=10???
    By fredsilvester93 in forum C Programming
    Replies: 2
    Last Post: 11-22-2011, 12:46 PM
  2. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  3. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  4. Difference between const char * and char const *
    By explorecpp in forum C Programming
    Replies: 4
    Last Post: 08-09-2008, 04:48 AM
  5. Replies: 1
    Last Post: 06-30-2008, 09:08 AM

Tags for this Thread